package com.studentsys.servlet;

import com.studentsys.service.RegistService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/regist")
public class RegistServlet extends HttpServlet {
    private RegistService rs = new RegistService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");

        //获取数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        int regist = rs.regist(username, password);
        System.out.println(regist);
        if (regist>0){

            resp.sendRedirect("login.jsp");
        }else{
            resp.sendRedirect("regist.jsp");
        }
    }

    public void checkUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean flag = rs.checkUser(username);
        //响应给客户端
        resp.getWriter().print(flag);

    }
}
